<?php
/**
 * Introduction: 订单接口
 * @author: 王龙
 * @date: 2016/4/18 17:37
 * @email: wanglong@cdlinglu.com
 */


namespace App\Controller\Order;

use Common\Controller\App\SignController;
use Common\Logic\DistributionLogic;
use Common\Logic\MemberLogic;
use Common\Logic\OrderLogic;

class IndexController extends SignController
{
    /**
     * Introduction: 订单列表
     * @author: 杨陈鹏
     * @email: yangchenpeng@cdlinglu.com
     */

    public function lists()
    {

        $where = I('post.data');

        $where['uid'] = self::$uid;
        $where['is_delete'] = 1;
        $where['_string']   = 'status != 7';

        $logic = D('Common/Order', 'Logic');

        $fields = 'id,number,status,create_time,order_price_total,freight,volume_id';

        $page = I('post.page');

        outPut($logic::getData($where,$fields,'create_time desc,status asc',$page));

    }


    /**
     * Introduction: 确认订单
     * @author: 杨陈鹏
     * @date: 2016/4/19 18:50
     * @email: yangchenpeng@cdlinglu.com
     */
    public function comfirm()
    {

        $data = I('post.data');


        $logic = D('Common/Order', 'Logic');

        $data['uid'] = self::$uid;

        outPut($logic::comfirm($data));

    }

    /**
     * Introduction: 计算订单运费
     * @author: 杨陈鹏
     * @date: 2016/4/19 18:50
     * @email: yangchenpeng@cdlinglu.com
     */
    public function calcFreight()
    {

        $data = I('post.data');

        $goods_logic = D('Common/Goods', 'Logic');

        foreach ($data['goods'] as $k => $v) {

            $info = [];
            $info = $v;
            $info['addr_id'] = $data['addr_id'];
            $info['city_id'] = $data['addr_id'];
            $info['county_id'] = $data['addr_id'];
            $info['province_id'] = $data['province_id'];



            $freight = $goods_logic::freight($info);
            $result[] = $freight['data'];

        }

        $total = 0;
        foreach($result as $k => $v){

            $total += $v['total'];

        }

        $res['total'] = $total;
        $res['volume'] = 0;
        if( $data['volume_id'] )
        {

            $volume_logic = D('Common/Volume','Logic');

            $res_data = $volume_logic::$model->where(['id'=>$data['volume_id']])->find();

            if($res_data && $res_data != 2 && $res_data['end_time'] >= NOW_TIME)
                $res['volume'] = $res_data['price'];

        }
        $res['info'] = $result;


        outPut(['code' => 200, 'data' => $res]);

    }

    /**
     * Introduction: 创建订单
     * @author: 杨陈鹏
     * @date: 2016/4/19 18:50
     * @email: yangchenpeng@cdlinglu.com
     */

    public function create()
    {

        $data = I('post.data');

        $logic = D('Common/Order', 'Logic');

        outPut($logic::create($data, self::$uid),true);


    }

    /**
     * Introduction: 获取快递公司
     * @author: 杨陈鹏
     * @email: yangchenpeng@cdlinglu.com
     */
    public function expressList()
    {

        $page = I('post.page',1);

        $nums = I('post.nums',20);

        $logic = D('Common/Express', 'Logic');


        outPut($logic::getData(['state' => 2], 'id,name','order_by asc',$page,$nums));

    }

    /**
     * Introduction: 计算订单总价
     * @author: 杨陈鹏
     * @email: yangchenpeng@cdlinglu.com
     */
    public function calcOrderTotalPrice()
    {

        $data = I('post.data');


        $logic = D('Common/Order','Logic');

        outPut($logic::calcOrderTotalPrice($data['order_id']),true);

    }
    /**
     * Introduction: 获取订单详情
     * @author: 杨陈鹏
     * @email: yangchenpeng@cdlinglu.com
     */
    public function detail()
    {

        $data = I('post.data');

        $logic = D('Common/Order','Logic');

        $order_id = $data['order_id'];

        outPut($logic::detail($order_id));


    }
    /**
     * Introduction: 取消订单
     * @author: 杨陈鹏
     * @email: yangchenpeng@cdlinglu.com
     */



    public function cancel()
    {

        $data = I('post.data');

        $logic = D('Common/Order','Logic');

        $id = $data['order_id'];

        $datas['status'] = 6;
        $datas['close_time'] = NOW_TIME;

        //解冻资金
        $logic::thawCapital($id);

        outPut($logic::update(['id' => $id], $datas));


    }

    /**
     * Introduction: 删除订单
     * @author: 杨陈鹏
     * @email: yangchenpeng@cdlinglu.com
     */
    /*

var data = {

        data:{'order_id': 343}

    }

     */



    public function delete()
    {

        $data = I('post.data');

        $logic = D('Common/Order','Logic');

        $id = $data['order_id'];

        outPut($logic::updateStatus($id,7));


    }

    /**
     * Introduction: 确认收货
     * @author: 杨陈鹏
     * @email: yangchenpeng@cdlinglu.com
     */
    public function receipt()
    {

        $data = I('post.data');

        $logic = new OrderLogic();
        $distribution_logic = new DistributionLogic();

        $id = $data['order_id'];

        //计算分销提成
        $distribution_logic::divided($id);

        $datas['receipt_time'] = NOW_TIME;
        $datas['status'] = 4;
        outPut($logic::update([ 'id' => $id ],$datas));


    }
    /**
     * Introduction: 订单流程
     * @author: 杨陈鹏
     * @email: yangchenpeng@cdlinglu.com
     */
    public function process()
    {

        $where = I('post.data');


        $logic = D('Common/Order','Logic');

        outPut($logic::process($where['order_id']));


    }


}